<?php
namespace app\index\controller;

use app\index\model\FlowLogModel;
use app\index\model\UserModel;
use app\index\model\WarnModel;
use app\index\service\OptService;
use app\index\service\UserService;
use app\index\service\WarnService;
use think\Db;
use think\Exception;
use think\Request;

class Warn extends Common
{

    public function list_warn(Request $request){
        $param = $request->post();

        $where[] = ['warn.comid','=',$this->userinfo['comid']];
        if(isset($param['type']) && !empty($param['type'])){
            $where[] = ['warn.type','=', $param['type']];
        }

        $data['data'] = WarnModel::alias('warn')
            ->field('warn.*')
            ->where($where)
            ->order('warn.sort ASC')
            ->select()->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function edit_warn(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['quen'] == 'w'){
                if(!isset($param['quenweek'])){
                    return outputJson(4000, '请选择预警日期');
                }else{
                    $param['quenweek'] = implode(',', $param['quenweek']);
                    $param['quendate'] = '';
                }
            }elseif($param['quen'] == 'm'){
                if(!isset($param['quendate'])){
                    return outputJson(4000, '请选择预警日期');
                }else{
                    $param['quenweek'] = '';
                    $param['quendate'] = implode(',', $param['quendate']);
                }
            }else{
                $param['quenweek'] = '';
                $param['quendate'] = '';
            }
            if(!isset($param['duier'])){
                return outputJson(4000, '请选择预警对象');
            }else{
                if(in_array('pointer', $param['duier'])){
                    if(!isset($param['zhier']) || empty($param['zhier'])){
                        return outputJson(4000, '请选择指定人员');
                    }
                }else{
                    $param['zhier'] = '';
                }

                $param['duier'] = implode(',', $param['duier']);
            }

            if(isset($param['state']) && $param['state'] == 'on'){
                $param['state'] = 1;
            }else{
                $param['state'] = 0;
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            unset($param['token']);

            Db::startTrans();
            try {
                WarnModel::update($param);

                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'warn', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'warn', $param['id'], '预警信息');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = WarnModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['updated_user_name'] = UserModel::get_realname_by_pk($data['data']['updated_user']);

            $data['duitemp'] = (new WarnService())->get_warn_duitemp();
            $data['data']['zhier_info'] = (new UserService())->get_users_part_arr($data['data']['zhier'], 'user');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


}
